package com.jiuling.apis.controller;

import com.jiuling.apis.model.Hmodel.TSysUser;
import com.jiuling.apis.model.Pmodel.User;
import com.jiuling.apis.service.basic.UserServiceI;
import com.jswzc.framework.controller.BaseController;
import com.jswzc.framework.entity.MessageBox;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*************************************************************************
 * 版本：         V1.0  
 *
 * 文件名称 ：UserController.java 描述说明 ：
 * 
 * 创建信息 : create by 刘佳佳 on 2016-6-14 下午3:11:14  修订信息 : modify by ( ) on (date) for ( )
 * 
 * 版权信息 : Copyright (c) 2015 扬州北斗软件有限公司
 **************************************************************************/
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserServiceI userService;

    //@Autowired
    //private SysUserServiceI sysUserService;

    // 定义address对象
    @InitBinder("user")
    public void initBinderAddress(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
    }

	/**
	 * 用户登录系统
	 * @param mv
	 * @return
	 */
	@RequestMapping("toHome")
	public ModelAndView toHome(HttpServletRequest request,ModelAndView mv,User user)throws Exception{
        String msg="";
	    try {
			TSysUser loginUser = userService.doLogin(user);
			request.getSession().setAttribute("loginUser", loginUser);
			request.getSession().setAttribute("userId", loginUser.getId());
			request.getSession().setAttribute("identity", loginUser.getIdentity());
			request.getSession().setAttribute("identityCode", loginUser.getIdentityCode());
			mv.setViewName("main/index");
        }catch (RuntimeException e){
            mv.addObject("msg",e.getMessage());
            mv.setViewName("../login/login_v2");
        }catch (Exception e){
			msg="登录异常!";
			mv.addObject("msg",msg);
			mv.setViewName("../login/login_v2");
		}
		return mv;
	}

	@RequestMapping("toRegist")
	public ModelAndView toRegist(ModelAndView mv)throws Exception{
		mv.setViewName("../login/regist");
		return mv;
	}

	@RequestMapping("doRegist")
	public void doRegist(HttpServletRequest request,HttpServletResponse response,User user)throws Exception{
		MessageBox mb = getBox();
		try {
			userService.doRegist(user);
		}catch (RuntimeException e){
			mb.setSuccess(false);
			mb.setMsg(e.getMessage());
		}catch (Exception e){
			mb.setSuccess(false);
			mb.setMsg("异常");
		}
		writeJson(mb,response);
	}
	
	/**
	 * 用户退出系统
	 * @return
	 * 清除session退出系统
	 */
	@RequestMapping("toLogin")
	public String toLogin(HttpServletResponse response){
		return "redirect:/login/login_v2.jsp";
	}



    //@RequestMapping(value = "/modifyPwd")
    //public void modifyPwd(HttpServletResponse response,String oldPwd,String newPwd2) throws Exception
    //{
    //    Map<String, Object> map = new HashMap<String, Object>();
    //    User sessionuser = (User)getShiroAttribute("user");
    //    User user=userService.findByName(sessionuser.getLogin_name());
    //    String oldPwdMD5=MD5.md5(oldPwd.trim());
    //    if(user.getPassword().equals(oldPwdMD5)){
    //        boolean state=userService.changePwd(MD5.md5(newPwd2),user.getId());
    //        if(state){
    //            map.put("isok", true);
    //            map.put("msg", "修改成功！");
    //        }else{
    //            map.put("isok", false);
    //            map.put("msg", "修改失败！");
    //        }
    //    }else{
    //        map.put("isok", false);
    //        map.put("msg", "原密码输入不正确！");
    //    }
    //
    //    writeJson(map, response);
    //}

    //@RequestMapping("toModifyUser")
    //public ModelAndView toModifyUser(ModelAndView mv) throws Exception{
    //    String identity_code=(String)getShiroAttribute("identity_code");
    //    User user = (User)getShiroAttribute("user");
    //    int identity =user.getIdentity();
    //    SysUser user2 = sysUserService.getUserByCode(user.getId());
    //    mv.addObject("sysUser",user2);
    //    if(identity==1){    //管理员
    //        mv.setViewName("main/admin_edit");
    //    }else if(identity==2){   //代理商
    //        Pagent ag=agentService.findById(user.getIdentity_code());
    //        mv.addObject("agent",ag);
    //        mv.setViewName("main/agent_edit");
    //    }else if(identity==3){   //商户
    //        Pbrand pbrand=merchantService.findById(user.getIdentity_code());
    //        mv.addObject("brand",pbrand);
    //        mv.addObject("cityId",pbrand.getCity());//回写的市id
    //        mv.setViewName("main/brand_edit");
    //    }
    //    mv.addObject("identity_code",identity_code);
    //    mv.addObject("type","1");
    //
    //    return mv;
    //}
}
